package com.glodon.paas.activity.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.paas.activity.biz.ActivityBiz;
import com.glodon.paas.activity.dao.domain.ActivityEntity;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.util.DateUtil;

@Controller
@RequestMapping("/")
public class ActivityController {

    public static final Logger LOGGER = LoggerFactory.getLogger(ActivityController.class);
    @Autowired
    ActivityBiz                activityBiz;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView query(
                    HttpServletRequest request, 
                    @RequestParam(value = "receiverId", required = false) String receiverId,
                    @RequestParam(value = "appId", required = false) String appId,
                    @RequestParam(value = "moduleId", required = false) String moduleId,
                    @RequestParam(value = "activityType", required = false) String activityType,
                    @RequestParam(value = "itemId", required = false) String itemId,
                    @RequestParam(value = "itemType", required = false) String itemType,
                    @RequestParam(value = "subItemId", required = false) String subItemId,
                    @RequestParam(value = "actorId", required = false) String actorId,
                    @RequestParam(value = "earlierTime", required = false) String earlierTime,
                    @RequestParam(value = "laterTime", required = false) String laterTime,
                    @RequestParam(value = "count", required = false, defaultValue = "50") String count,
                    @RequestParam(value = "needRender", required = false, defaultValue = "true")Boolean needRender,
                    ModelMap model
                    ) {

        // String userId = SecurityUtils.getCurrentUser().getId();
        if (receiverId == null) {
            receiverId = "hc";
        }
        if (appId == null) {
            appId = "102";
        }
        Map<String, String> params = new HashMap<String, String>();
        params.put("receiverId", receiverId);
        params.put("appId", appId);
        params.put("moduleId", moduleId);
        params.put("activityType", activityType);
        params.put("itemId", itemId);
        params.put("itemType", itemType);
        params.put("subItemId", subItemId);
        params.put("actorId", actorId);
        params.put("earlierTime", earlierTime);
        params.put("laterTime", laterTime);
        params.put("count", count);
        params.put("needRender", String.valueOf(needRender));

        List<ActivityEntity> ls = activityBiz.query(params);
        model.addAttribute(StringConst.ITEMS, ls);
        return new ModelAndView("activity", model);
    }

    // http://127.0.0.1:8080/activity/serverinfo.json?testString=111
    @RequestMapping(value = "serverinfo", method = RequestMethod.GET)
    public Object serverinfo(
                  @Context HttpServletRequest request,
                  @Context HttpServletResponse response,
                  @QueryParam("testString") String testString
                  ) {

        Map<String, Object> param = new HashMap<String, Object>();
        param.put("version", "activicy-1.0.0 ");
        param.put("submitDate", "2012-12-04");
        param.put("currentTime", DateUtil.formatDate(new Date(), DateUtil.DAFAULT_DATETIME_FORMAT));
        param.put("testString", testString);
        return param;
    }
}
